﻿
CREATE PROCEDURE [dbo].[exbug_ListaCheltuieliDepartamente]
	@CodUnitate smallint, 
	@DeLaData datetime,
	@PanaLaData datetime,
	@IDJurnal int
AS
BEGIN
	SET NOCOUNT ON;

	SELECT CASE 
	WHEN d.IDTipDepartament In (16,18) THEN 0 
	WHEN d.IDTipDepartament In (22) THEN 1 
	ELSE 2 END ord, d.Nume,td.Nume TipDepartament,
	sum(e.Valoare) Chelt_totale
	FROM Departamente d JOIN TipDepartament td ON td.IDTipDepartament = d.IDTipDepartament
	JOIN ExecutieCheltuieli e ON e.IDDepartament = d.IDDepartament AND d.Activ=1 and e.CodUnitate = @CodUnitate
	WHERE d.CodUnitate = @CodUnitate AND Data>=@DeLaData AND Data <=@PanaLaData
	GROUP BY d.IDTipDepartament,td.Nume,d.Nume
	having SUM(Valoare)<>0 
	ORDER BY ord,TipDepartament,d.Nume
END